<?php

namespace app\logic\admin;

class L_friendLinkGroup
{
    /*获取 全部分组,可指定递归次数
     *
     */
    function getAllJson($paramObj = "")
    {
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = array();

        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'count' => 'require', // 最大递归次数
            );

            $dataDefault = array();
            $dataDefault["field"] = "*";
            $dataDefault["format"] = "1";
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $paramsObj->dataDefault = $dataDefault;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {

            $groupList = array();
            $checkResult = app("S_Admin_friendLinkGroup")->getAll(getParamsObj());
            if ($checkResult["error_no"] == 0) {
                $groupList = $checkResult["result"];
            }
            $Json = $this->tree($groupList, 0, 0, $dataObj->request->count);
            $final_arr["result"] = $Json;
        }


        return $final_arr;
    }

    function tree($groupList, $pid, $count, $countMax)
    {
        $count = $count++;
        $arr = [];
        if ($count == $countMax) {
            return $arr;
        }
        foreach ($groupList as $k => $v) {
            if ($v["pid"] == $pid) {
                $a = $v;
                $a["children"] = $this->tree($groupList, $pid, $count, $countMax);
                $arr[] = $a;
            }
        }
        return $arr;
    }

}